package xiao.ke.serviceinvoker1.service;/**
 * @author XuXiaoK
 * @create 2020-01-21 21:05
 */

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import xiao.ke.serviceinvoker1.entity.Person;

/**
 *@Author XiaoKe
 *@Date 2020/1/21 21:05
 *@Description TODO
 */
@Component
public class PersonService {

    @Autowired
    private RestTemplate restTemplate;


    @CacheResult
    @HystrixCommand(fallbackMethod = "getPersonFallback")
    public Person getPerson(Integer id) {
        // 使用RestTemplate调用Eureka服务
        System.out.println("执行方法");
        Person person = restTemplate.getForObject("http://service-provider1/person/{personId}",
                Person.class, id);
        return person;
    }




    public Person getPersonFallback(Integer id) {
        Person xiaoke = Person.builder().id(0).name("ke").age(-1).build();
        return xiaoke;
    }

    @CacheResult
    @HystrixCommand
    public Person getPerson1(Integer id) {
        System.out.println("执行");
        Person xiaoke = Person.builder().id(0).name("ke").age(-1).build();
        return xiaoke;
    }
}

